package com.prs.crm.service.doc.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

import com.prs.crm.context.AppContext;
import com.prs.crm.context.FileUtil;
import com.prs.crm.dao.doc.DeptDocDao;
import com.prs.crm.domain.doc.DeptDoc;
import com.prs.crm.domain.doc.DeptDocType;
import com.prs.crm.domain.doc.FileType;
import com.prs.crm.service.doc.DeptDocService;
import com.prs.crm.service.impl.BaseServiceImpl;

public class DeptDocServiceImp extends BaseServiceImpl<DeptDoc, DeptDocDao> implements
		DeptDocService {

	@Override
	public void saveDeptDoc(DeptDoc deptDoc, File file, String fileName) {
		// TODO Auto-generated method stub
		deptDoc.setAttach(FileUtil.getInstance().createAttach(file, fileName));
		deptDoc.setRecorder(AppContext.getAccountContext().currentUser());
		deptDoc.setRecordTime(new Date());
		FileType ft = FileUtil.getInstance().getFileType(fileName);
		deptDoc.setFileType(ft);
		this.getDao().save(deptDoc);
	}

	@Override
	public Collection<DeptDoc> listDeptDocByType(DeptDocType deptDocType) {
		// TODO Auto-generated method stub
		return this.getDao().listDeptDocByType(deptDocType);
	}

	@Override
	public Collection<DeptDocType> convertToDeptDocType(String ids) {
		// TODO Auto-generated method stub
		try {
			Collection<DeptDocType> depts = new ArrayList<DeptDocType>();
			for(String id : ids.split(",")){
				DeptDocType dept = new DeptDocType();
				dept.setId(Integer.parseInt(id));
				depts.add(dept);
			}
			return depts;
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
	}

	@Override
	public Collection<DeptDoc> convertToDeptDoc(Integer[] ids) {
		// TODO Auto-generated method stub
		
		try {
			Collection<DeptDoc> depts = new ArrayList<DeptDoc>();
			for(Integer id : ids){
				DeptDoc dept = this.getDao().get(id);
				depts.add(dept);
			}
			return depts;
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
		
	}

	@Override
	public Collection<DeptDoc> getDeptDocByIds(Integer[] ids) {
		// TODO Auto-generated method stub
		return this.getDao().getDeptDocByIds(ids);
	}



}
